package com.example.disease.ui.home;

import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.example.disease.R;
import com.example.disease.databinding.FragmentHomeBinding;

public class HomeFragment extends Fragment {
    private static final String TAG = "HomeFragment";

    private FragmentHomeBinding binding;
    private static final int INTERNET_PERMISSION_REQUEST_CODE = 1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        HomeViewModel homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        final TextView textView = binding.textViewEdittext;
        homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);

        // 获取输入框和登录按钮
        EditText etUsername = binding.usernameEdittext;
        EditText etPassword = binding.passwordEdittext;
        Button btnLogin = binding.loginButton;

        // 检查网络权限
        if (ContextCompat.checkSelfPermission(requireContext(), android.Manifest.permission.INTERNET)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(requireActivity(),
                    new String[]{android.Manifest.permission.INTERNET},
                    INTERNET_PERMISSION_REQUEST_CODE);
        }

        // 设置登录按钮的点击事件
        btnLogin.setOnClickListener(v -> {
            String username = etUsername.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            // 简单的登录验证逻辑，这里只是示例，实际应用中需要与服务器交互
            if (username.equals("admin") && password.equals("123456")) {
                Toast.makeText(requireContext(), "登录成功", Toast.LENGTH_SHORT).show();
                // 登录成功后跳转到 DashboardFragment
                FragmentManager fragmentManager = getFragmentManager();
                if (fragmentManager != null) {
                    NavController navController = Navigation.findNavController(v);
                    //跳转页面
                    navController.navigate(R.id.homeFragment);
                }
            } else {
                Toast.makeText(requireContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show();
            }
        });

        return root;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == INTERNET_PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 权限已授予
            } else {
                // 权限被拒绝，处理相应逻辑，比如提示用户
                Toast.makeText(requireContext(), "网络权限被拒绝，无法正常使用应用", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}